package com.yanqu.road.server.cmd.activity.shuihu;

import com.yanqu.road.entity.activity.shuihu.ShuiHuUserData;
import com.yanqu.road.pb.activity.ShuiHuActivityProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.shuihu.ShuiHuModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.shuihu.ShuiHuMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

@Cmd(code = GameProtocol.S_SHUIHU_ACTIVITY_GET_COMB_REWARD, desc = "领取组合奖励")
public class GetCombRewardCmd extends AbstractCommand {

    private int combId;
    private int times;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        ShuiHuActivityProto.GetShuiHuCombRewardReqMsg reqMsg = ShuiHuActivityProto.GetShuiHuCombRewardReqMsg.parseFrom(packet.getBytes());
        combId = reqMsg.getCombId();
        times = reqMsg.getTimes();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if (!ActivityMgr.activityInShowTime(ShuiHuMgr.getActivityInfo())) {
            ret = GameErrorCode.E_SHUIHU_ACTIVITY_NO_OPEN;
            return;
        }

        ShuiHuModule shuiHuModule = player.getModule(ShuiHuModule.class);
        ShuiHuUserData userData = shuiHuModule.getUserData();
        if (userData == null) {
            ret = GameErrorCode.E_SHUIHU_ACTIVITY_NO_UNLOCK;
            return;
        }

        if (times <= 0) {
            ret = GameErrorCode.E_SHUIHU_ACTIVITY_COUNT_ERROR;
            return;
        }

        ret = shuiHuModule.getCombReward(combId, times);
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            ShuiHuActivityProto.GetShuiHuCombRewardRespMsg.Builder builder = ShuiHuActivityProto.GetShuiHuCombRewardRespMsg.newBuilder();
            builder.setRet(ret);
            player.sendPacket(ClientProtocol.U_SHUIHU_ACTIVITY_GET_COMB_REWARD, builder);
        }
    }
}
